package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckedTextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants.SortByOrder;
import com.sec.android.app.myfiles.info.AppConstants.SortByType;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.log.SamsungAnalyticsLog;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;

public class SortbyDialogFragment extends AbsDialogFragment {
    private CheckedTextView mRadioTime;
    private CheckedTextView mRadioType;
    private CheckedTextView mRadioName;
    private CheckedTextView mRadioSize;
    private CheckedTextView mRadioDescending;
    private CheckedTextView mRadioAscending;

    private static final int ID_TIME = R.id.sortby_time_radio_button;
    private static final int ID_TYPE = R.id.sortby_type_radio_button;
    private static final int ID_NAME = R.id.sortby_name_radio_button;
    private static final int ID_SIZE = R.id.sortby_size_radio_button;
    private static final int ID_DES = R.id.sortby_descending_radio_button;
    private static final int ID_ASC = R.id.sortby_ascending_radio_button;

    private int mType;
    private int mOrder;

    public static SortbyDialogFragment getInstance() {
        return new SortbyDialogFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(this, "onSaveInstanceState()");

        DialogFragmentUtils.setTargetFragment(this);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected Dialog _createDialog() {
        Log.d(this, "onCreateDialog()");

        View v = LayoutInflater.from(getActivity()).inflate(R.layout.sortby_dialog_common, null);
        initView(v);

        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setTitle(R.string.sort_by);
        dialog.setView(v);
        dialog.setPositiveButton(R.string.menu_done, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                StringBuilder sb = new StringBuilder();
                if (!getSortByTypeForLiteral(mType).isEmpty() && !getSortByOrderForLiteral(mOrder).isEmpty()) {
                    sb.append(getSortByTypeForLiteral(mType)).append("-").append(getSortByOrderForLiteral(mOrder));
                }
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.SORTBY_POPUP, SamsungAnalyticsLog.Event.DONE, sb.toString(), null);
                PreferenceUtils.setSortByType(getActivity(), mNavigationManager.getCurInfo(), mType);
                PreferenceUtils.setSortByOrder(getActivity(), mNavigationManager.getCurInfo(), mOrder);
                if (getTargetFragment() != null)
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
            }
        });
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SamsungAnalyticsLog.sendLog(SamsungAnalyticsLog.ScreenPath.SORTBY_POPUP, SamsungAnalyticsLog.Event.CANCEL, null);
            }
        });

        Context context = getActivity();

        if (UiUtils.isTalkBackEnabled(context)) {
            View sortbyHeader = v.findViewById(R.id.sortby_subHeader);
            sortbyHeader.setFocusable(true);
            sortbyHeader.setContentDescription(UiUtils.getHomeDescription(context, R.string.in_order, R.string.tts_header));
        }

        return dialog.create();
    }

    private void initView(View v) {
        mRadioTime = (CheckedTextView) v.findViewById(ID_TIME);
        mRadioType = (CheckedTextView) v.findViewById(ID_TYPE);
        mRadioName = (CheckedTextView) v.findViewById(ID_NAME);
        mRadioSize = (CheckedTextView) v.findViewById(ID_SIZE);
        mRadioDescending = (CheckedTextView) v.findViewById(ID_DES);
        mRadioAscending = (CheckedTextView) v.findViewById(ID_ASC);

        mRadioTime.setOnClickListener(mTypeRadioClickListener);
        mRadioType.setOnClickListener(mTypeRadioClickListener);
        mRadioName.setOnClickListener(mTypeRadioClickListener);
        mRadioSize.setOnClickListener(mTypeRadioClickListener);
        mRadioDescending.setOnClickListener(mOrderRadioClickListener);
        mRadioAscending.setOnClickListener(mOrderRadioClickListener);

        initTypeRadioBtn();
        initOrderRadioBtn();
    }

    private void initTypeRadioBtn() {
        mType = PreferenceUtils.getSortByType(getActivity(), mNavigationManager.getCurInfo());
        setTypeRadioBtn(mType);
        setFocus(mType);
    }

    private void initOrderRadioBtn() {
        mOrder = PreferenceUtils.getSortByOrder(getActivity(), mNavigationManager.getCurInfo());
        setOrderRadioBtn(mOrder);
    }

    public void setTypeRadioBtn(int type) {
        mRadioTime.setChecked(type == SortByType.TIME);
        mRadioType.setChecked(type == SortByType.TYPE);
        mRadioName.setChecked(type == SortByType.NAME);
        mRadioSize.setChecked(type == SortByType.SIZE);
    }

    public void setOrderRadioBtn(int order) {
        mRadioAscending.setChecked(order == SortByOrder.ASC);
        mRadioDescending.setChecked(order == SortByOrder.DESC);
    }

    private void setFocus(int type) {
        View focusView = null;
        switch (type) {
            case SortByType.TIME:
                focusView = mRadioTime;
                break;
            case SortByType.TYPE:
                focusView = mRadioType;
                break;
            case SortByType.NAME:
                focusView = mRadioName;
                break;
            case SortByType.SIZE:
                focusView = mRadioSize;
                break;
        }

        if (focusView != null) {
            focusView.requestFocus();
        }
    }

    public OnClickListener mTypeRadioClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            mType = getSortByTypeFromViewId(viewId);
            setTypeRadioBtn(mType);
        }
    };

    public OnClickListener mOrderRadioClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            mOrder = getSortByOrderFromViewId(viewId);
            setOrderRadioBtn(mOrder);
        }
    };

    public int getSortByTypeFromViewId(int viewId) {
        int type = 0;
        switch (viewId) {
            case ID_TIME:
                type = SortByType.TIME;
                break;
            case ID_TYPE:
                type = SortByType.TYPE;
                break;
            case ID_NAME:
                type = SortByType.NAME;
                break;
            case ID_SIZE:
                type = SortByType.SIZE;
                break;
            default:
                break;
        }

        return type;
    }

    public String getSortByTypeForLiteral(int type) {
        String sortByType = "";
        switch (type) {
            case SortByType.TIME:
                sortByType = "Time";
                break;
            case SortByType.TYPE:
                sortByType = "Type";
                break;
            case SortByType.NAME:
                sortByType = "Name";
                break;
            case SortByType.SIZE:
                sortByType = "Size";
                break;
            default:
                break;
        }

        return sortByType;
    }

    public int getSortByOrderFromViewId(int viewId) {
        int order = 0;
        switch (viewId) {
            case ID_DES:
                order = SortByOrder.DESC;
                break;
            case ID_ASC:
                order = SortByOrder.ASC;
                break;
            default:
                break;
        }

        return order;
    }

    public String getSortByOrderForLiteral(int order) {
        String sortByOrder = "";
        switch (order) {
            case SortByOrder.DESC:
                sortByOrder = "descending";
                break;
            case SortByOrder.ASC:
                sortByOrder = "ascending";
                break;
            default:
                break;
        }

        return sortByOrder;
    }
}
